package com.partners.controller.mp;

import com.partners.configure.wechatmp.WxMpConfiguration;
import com.partners.dao.dto.NewsInputDTO;
import com.partners.dao.dto.TemplateDTO;
import com.partners.model.ApiResult;
import com.partners.service.TemplateService;
import com.partners.utils.ApiResultUtil;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpKefuService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 发送模板消息
 * @author liangyongle
 * @date 2019-01-10
 */
@RestController
@RequestMapping("template")
public class TemplateController {
    public final static String appId = "wx1b38cf3729aaa2a7";


    @Resource
    private TemplateService templateServcie;

    /**
    * 发送模板消息
    *
    * */
    @RequestMapping("send")
    public ApiResult sendTemplates(){
        String openId  = "oR3V81QxrDYq9mCgidJLH23ke30w";
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = new Date(System.currentTimeMillis());
        TemplateDTO templateDTO = new TemplateDTO();
        templateDTO.setFirst("这是标题");
        templateDTO.setKeyword1("这是内容");
        templateDTO.setKeyword2(format.format(nowTime));
        templateDTO.setRemark("这是备注的信息");
        templateServcie.sendHanleTemplate(templateDTO,openId);
        return ApiResultUtil.ok(0);
    }

    @RequestMapping("text")
    public ApiResult sendTextMessage(){
        WxMpKefuMessage message = new WxMpKefuMessage();
        message.setMsgType(WxConsts.KefuMsgType.TEXT);
        message.setToUser("oR3V81QxrDYq9mCgidJLH23ke30w");
        message.setContent("欢迎欢迎");
        final WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
        boolean result = false;
        try {
            result = wxMpService.getKefuService().sendKefuMessage(message);
            System.out.println("发送成功");
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return ApiResultUtil.ok(0);
    }

    /**
     * 发送图文消息
     *
    * */
    @RequestMapping("news")
    public ApiResult sendNewsMessage(){
        NewsInputDTO newsInputDTO = new NewsInputDTO();
        String openId = "oR3V81QxrDYq9mCgidJLH23ke30w";
        newsInputDTO.setUrl("http://baidu.com");
        newsInputDTO.setPicUrl("http://img12.360buyimg.com/n1/jfs/t7354/60/2363147400/323049/80fc3f35/59ae0ccdN2857893e.jpg");
        newsInputDTO.setDescription("点击注册");
        newsInputDTO.setTitle("合伙人注册");
        templateServcie.sendNewsMessage(newsInputDTO,openId);
        return ApiResultUtil.ok(0);
    }

     /**
      * 测试发图文
      * */
     @RequestMapping("sendnews")
     public ApiResult sendNewsMessageByPic(){

       return null;
     }

     @RequestMapping("sends")
     public ApiResult sendTemplate(){
        final WxMpService wxMpService = WxMpConfiguration.getMpServices().get(appId);
        //实例化模板对象
        WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
        //设置模板ID
        wxMpTemplateMessage.setTemplateId("WbOhTRrGM7K_rjteFrnOZCp9toddGhyxxtCduyBTx4I");
        //设置发送给哪个用户
        wxMpTemplateMessage.setToUser("oR3V81baBeAiEjL6juAkYl1GuxvM");
        //构建消息格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowTime = new Date(System.currentTimeMillis());
        List<WxMpTemplateData> list= Arrays.asList(
            new WxMpTemplateData("first","模板标题"),
            new WxMpTemplateData("keyword1","这是内容"),
            new WxMpTemplateData("keyword2",format.format(nowTime)),
            new WxMpTemplateData("remark","备注信息")
        );
        //放进模板对象。准备发送
        wxMpTemplateMessage.setData(list);
        try {
            //发送模板
            wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return null;
    }


    @RequestMapping("test")
    public ApiResult testTemplate(){
        templateServcie.sendTemplateTask();
        return ApiResultUtil.ok(0);
    }


}
